function Combox_Project() {
	$.ajax({
		type : "POST",
		url : "../ProjectController/findByCondition.action",
		data : {
			"projectParentId":-1,
		},
		success : function(data) {
			var html ="";
			$.each(JSON.parse(data), function(j, o) {
				html += '<option value="' + o.projectId + '">' + o.projectName
						+ '</option>';
			});
			$("#ProjectCombox").html(html);
		},
		error : function(request) {
			toastr.error("多选框错误!");
		}
	});
}

function Combox_Type() {
	$.ajax({
		type : "POST",
		url : "../TitleTypeController/findByCondition.action",
		data : null,
		success : function(data) {
			var html = "";
			$.each(JSON.parse(data), function(j, o) {
				html += '<option value="' + o.titleTypeid + '">' + o.titleTypename
						+ '</option>';
			});
			$("#TitleCombox").html(html);
			$(".js-example-basic-multiple").select2();
		},
		error : function(request) {
			toastr.error("多选框错误!");
		}
	});
}

function queryParams(params) {
	return {
		projectName : $('#projectName').val(),
	};
}
$('#reset').click(function() {
	$('#projectName').val(""),
	$('#Project').bootstrapTable('refresh', {url : '../ProjectController/findByCondition.action'});

});
//通过ID插到类型名称
function findTitleContent(data1) {
  if(data1!=null||data1!=""){
	var data=data1.split(",");
	var a="";
  for(var i=0;i<data.length;i++){	 
	$.ajax({
		type : "POST",
		url : "../TitleTypeController/findByCondition.action",
		async : false,
		data :  
		{
			"titleTypeid": data[i],
		},
		success : function(d) {	
		
			var obj = eval('(' + d + ')');	
			a=a+obj[0].titleTypename+",";
			/*if(a.length<=10){
				
				
			}
			else if(a.length>10){
				a=a.substr(0,10)+obj[0].titleTypename+"..."+",";
			}
			*/
			if(i==data.length-1){
				a=a.substr(0,a.length-1);
			}
		},
		
	});
  }
  return '<a  style="width:60px; dislpay:inline;overflow:hidden; white-space:nowrap;color:#CD8500">'+a+'</a>';
}
  else{
	  return '无';
  }
}


$(function() {
	$("#ProjectForm").validate({
		rules : {
			projectName : "required",
		},
		errorPlacement : function(label, element) {
			$(element).tooltip('destroy'); /* 必需 */
			$(element).attr('title', $(label).text()).tooltip('show');
		},
	});
	Combox_Type();
	Combox_Project();
	init('Project', 500, 300, "projectId", "测评项目");
});

$("#insertProject").click(function() {
	$('#projectId').val(""),
	$('#projectName2').val(""), 
	$('#extend1').val(""),
	$("#TitleCombox").select2('val', '[]');
	$("#ProjectCombox").val("");
	$("#submitUpdateProject").css("display","none");
	$("#submitaddProject").css("display","block");
	$('#addProjectTitle').modal('show');
});

$("#modifyProjectForm").click(function() {
	var id = getProjectId("Project", 'projectId');
	if (id.length == 1) {
		 var str="";
		$.ajax({
			type:"post",
		    url:"../ProjectController/findByCondition.action", 
		    data:{ 
		    	"projectId" :id.join(','), 
		    	}, 
		    success:function(d){
		    	var obj=eval(''+d+'');
		    	str= obj[0].extend2;
			    var arr=str.split(',');
			       $('#projectName2').val(obj[0].projectName);
			       $('#extend1').val(obj[0].extend1);
			       $('#TitleCombox').select2('val', [arr]);
			       $('#projectId').val(obj[0].projectId);
		    },
		 error : function(request) { toastr.error("操作失败"); } });
		 
	}
	else{
		toastr.warning("请选择一条记录进行操作!");

	}
	$("#submitaddProject").css("display","none");
	$("#submitUpdateProject").css("display","block");
	$('#addProjectTitle').modal('show');
});
//增加
$("#submitaddProject").click(function() {
	var form = "#ProjectForm";
	var url = "../ProjectController/add.action";
	var type = "Project";
	submitProjectadd(form, url, type);
});
//修改
$("#submitUpdateProject").click(function(){
	var form = "#ProjectForm";
	var url = "../ProjectController/update.action";
	var type = "Project";
	submitProject_update(form, url, type);
});
function submitProjectadd(form, url, type) {

	var titleids = $("#TitleCombox").val() + "";
	$.ajax({
		type : "POST",
		url : url,
		data : {
			"projectId" : $("#projectId").val(),
			"projectName" : $("#projectName2").val(),
			"extend1" : $("#extend1").val(),
			"extend2" : titleids,
			"projectParentId":$("#ProjectCombox").val(),
		},
		success : function(data) {
			$('#projectId').val(""),
			$('#projectName2').val(""),
			$('#extend1').val(""),
			$("#TitleCombox").select2('val', '');
			$("#projectParentId").val(''),
			$('#addProjectTitle').modal('hide');
			window.location.href = "projectList.action";
			toastr.success("添加成功!");
		},
		error : function(request) {
		/*	toastr.error("操作失败哈哈哈哈哈哈哈哈或或或或或或或或或或或或或或");*/
		}
	});
}


function submitProject_update(form, url, type) {
	var titleids = $("#TitleCombox").val() + "";
	$.ajax({
		type : "POST",
		url : url,
		data : {
			"projectId" : $("#projectId").val(),
			"projectName" : $("#projectName2").val(),
			"extend1" : $("#extend1").val(),
			"extend2" : titleids,
			"projectParentId":$("#ProjectCombox").val(),
		},
		success : function(data) {

			var obj = JSON.parse(data);
			$('#' + type).bootstrapTable('updateByUniqueId', {
				id : obj.redata,
				row : obj.data
			});
			$('#addProjectTitle').modal('hide');
			toastr.success("修改成功!");
		},
		error : function(request) {
			toastr.error("操作失败!");
		}
	});
}

function getProjectId(type, parm) {

	return $.map($('#' + type).bootstrapTable('getSelections'), function(row) {
		return eval('row.' + parm);
	});
}
